package structure;


public class Test12 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

    }
    //leetcode 107 二叉树的层序遍历 Ⅱ
    class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> list = new ArrayList<List<Integer>>();
            Deque<TreeNode> deque = new ArrayDeque<>();
            if (root == null) {
                return list;
            }
            deque.push(root);
            while (!deque.isEmpty()) {
                List<Integer> liSt = new ArrayList<>();
                int len = deque.size();
                for (int i = 0; i < len; i++) {
                    TreeNode node = deque.removeLast();
                    if (node != null) {
                        liSt.add(node.val);
                        if(node.left != null){
                            deque.push(node.left);
                        }
                        if(node.right != null){
                            deque.push(node.right);
                        }
                    }
                }
                list.add(0,liSt);
            }
            return list;
        }
    }
    //leetcode 102 二叉树的层序遍历
    class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> list = new ArrayList<List<Integer>>();
            Deque<TreeNode> deque = new ArrayDeque<>();
            if (root == null) {
                return list;
            }
            deque.push(root);
            while (!deque.isEmpty()) {
                List<Integer> liSt = new ArrayList<>();
                int len = deque.size();
                for (int i = 0; i < len; i++) {
                    TreeNode node = deque.removeLast();
                    if (node != null) {
                        liSt.add(node.val);
                        if(node.left != null){
                            deque.push(node.left);
                        }
                        if(node.right != null){
                            deque.push(node.right);
                        }
                    }
                }
                list.add(liSt);
            }
            return list;
        }
    }
}
